<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="servlets.message.*"%>
<%@ page import="org.hibernate.SessionFactory"%>
<%@ page import="org.hibernate.Session"%>
<%@ page import="java.util.List"%>
<%@ page import="hibernate.model.*"%>
<%@ page import="hibernate.util.HibernateUtil"%>
<%@ page import="hibernate.method.*"%>
<%@ page import="hibernate.method.UserMethods"%>


<script> 
$(".subjects").click(function(e){
	 e.preventDefault();
	 var clickedID = $(this).attr('id');
	  $.ajax({
	       url: "../MessageDetails",
	       type: "POST",//type of posting the data
	       data: {
		       messageID: clickedID
	       },
	       dataType: 'json',
	       success: function (json) {
			var data = json;
			$("#msg").hide().fadeIn(1000).html("<table class=\"table\">");
			$("#msg").append("<thead></thead>");
			$("#msg").append("<tbody>");
		    $("#msg").append("<tr><td>Nadawca:</td><td>"+data.sender+"</td></tr>");
		    $("#msg").append("<tr><td>Data:</td><td>"+data.date+"</td></tr>");
		    $("#msg").append("<tr><td>Godzina:</td><td>"+data.time+"</td></tr>");
		    $("#msg").append("<tr><td>Temat:</td><td>"+data.subject+"</td></tr>");
		    $("#msg").append("<tr><td>Tresc:</td><td>"+data.content+"</td></tr>");
		    $("#msg").append("</tbody>");
		    $("#msg").append("</table>");
	       },
	       error: function(xhr, ajaxOptions, thrownError){
	       },
	       timeout : 15000
	  });

	});

</script>


<div class="page-header">
	<h2 class="page-title">Skrzynka nadawcza</h2>
</div>

<br />

<div id="content">
<%
					
						SessionFactory sessionFactory = HibernateUtil
								.getSessionAnnotationFactory();
						Session s = sessionFactory.openSession();
						s.beginTransaction();
						MessageMethods msg_m = new MessageMethods();
						 Integer user_id = (Integer) session.getAttribute("user_id");
							response.setContentType("text/html");
							if (user_id != null) {
						List<Message> msglist = msg_m.list_by_sender(sessionFactory, s, user_id);
						s.getTransaction().commit();
					%> 
<div class="table-responsive">			
<table class="table">
<thead>
<tr>
<td>Odbiorca</td>
<td>Temat </td>
<td>Data</td>
</tr>
</thead>
<%
		if (msglist.size() > 0) {
	for (Message m1 : msglist) {
		int receiver_id = m1.getReceiver_id();
		UserMethods u_m = new UserMethods();
		User receiver = u_m.id_to_user(sessionFactory, s, receiver_id);
		String receiver_nick = receiver.getUsername();
		String subject = m1.getSubject();
		String date = m1.getSend_date();
		String time = m1.getSend_time();
		%>
		<tbody>
<tr>
<td><% out.println(receiver_nick); %></td>
<td><% out.println("<a class=\"subjects\" id=\""+ m1.getMessage_id() + "\" href=\"#\">" + subject + "</a>"); %> </td>
<td><% out.println(date + " " + "godz. " +  time); %></td>
</tr>
</tbody>		
		<%
		}
		}
		}
		
		%>
		
		
		

</table>
</div>

<br/><br />
<div id="messageDetails">
<div class="page-header">
<h2 class="page-title">Szczególy wiadomosci</h2>
</div>
<div id="msg">Kliknij na temat wiadomosci, której szczegoly chcesz zobaczyc</div>
</div>


</div>
